package com.zsxb.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsxb.crm.dto.ReportDateDTO;
import com.zsxb.crm.mapper.*;
import com.zsxb.crm.pojo.Business;
import com.zsxb.crm.pojo.Clue;
import com.zsxb.crm.pojo.Contract;
import com.zsxb.crm.mapper.IndexMapper;
import com.zsxb.crm.pojo.Clue;
import com.zsxb.crm.service.ClueService;
import com.zsxb.crm.service.IndexService;
import com.zsxb.crm.vo.BaseInfo;
import com.zsxb.crm.vo.ClueReportVO;
import com.zsxb.crm.vo.ReportSalesVO;
import com.zsxb.crm.vo.businessChangeVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.zsxb.crm.vo.ClueTo20VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;




    //线索龙虎榜
    @Override
    public List<ClueTo20VO> getClueTO20(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<ClueTo20VO> clueTo20VO = indexMapper.getClueList(beginCreateTime, endCreateTime);
        return clueTo20VO;
    }
    /**
     * 基础数据
     */
    @Override
    public BaseInfo getBaseInfo(LocalDate beginCreateTime, LocalDate endCreateTime) {
        Integer cluesNum = clueMapper.queryNewClueByDate(beginCreateTime.atStartOfDay(), endCreateTime.atStartOfDay());
        Integer businessNum = businessMapper.selectCount(null);
        Integer contractNum = reportMapper.querycontractNumsByDate(beginCreateTime, endCreateTime);
        List<ReportSalesVO> reportSales = reportMapper.salesStatistics(beginCreateTime, endCreateTime);
        Integer salesAmount = reportSales.stream().map(ReportSalesVO::getOrderPrice).reduce(0, Integer::sum);
        List<ClueReportVO> clueReport = reportMapper.cluesStatistics(beginCreateTime, endCreateTime);
        Integer todayCluesNum = clueReport.stream().map(ClueReportVO::getNewClue).reduce(0, Integer::sum);

        LambdaQueryWrapper<Business> businessWrapper = new LambdaQueryWrapper<>();
        businessWrapper.between(Business::getCreateTime, beginCreateTime.minusDays(1), beginCreateTime.plusDays(1));
        Integer todayBusinessNum = businessMapper.selectCount(businessWrapper);

        LambdaQueryWrapper<Contract> contractWrapper = new LambdaQueryWrapper<>();
        contractWrapper.between(Contract::getCreateTime, beginCreateTime.minusDays(1), beginCreateTime.plusDays(1));
        Integer todayContractNum = contractMapper.selectCount(contractWrapper);

        List<ReportSalesVO> reportSale = reportMapper.salesStatistics(LocalDate.now(), LocalDate.now().plusDays(1));
        Integer todaySalesAmount = reportSale.stream().map(ReportSalesVO::getOrderPrice).reduce(0,Integer::sum);
        // clues  status : 1 待跟进 2 跟进中 3 待分配
        Integer tofollowedCluesNum = clueMapper.selectCount(new LambdaQueryWrapper<Clue>().eq(Clue::getStatus, 1));
        Integer tofollowedBusinessNum = businessMapper.selectCount(new LambdaQueryWrapper<Business>().eq(Business::getStatus, 1)) ;
        Integer toallocatedCluesNum = clueMapper.selectCount(new LambdaQueryWrapper<Clue>().eq(Clue::getStatus, 3));
        Integer toallocatedBusinessNum = businessMapper.selectCount(new LambdaQueryWrapper<Business>().eq(Business::getStatus, 3)) ;

        BaseInfo baseInfo = BaseInfo.builder().cluesNum(cluesNum).businessNum(businessNum).contractNum(contractNum).salesAmount(salesAmount)
                .todayCluesNum(todayCluesNum).todayBusinessNum(todayBusinessNum).todayContractNum(todayContractNum)
                .tofollowedCluesNum(tofollowedCluesNum).tofollowedBusinessNum(tofollowedBusinessNum)
                .toallocatedBusinessNum(toallocatedCluesNum).toallocatedCluesNum(toallocatedBusinessNum).build();
        return baseInfo;
    }

    @Override
    public List<businessChangeVO> businessChangeStatistics(ReportDateDTO dateDTO) {
        List<businessChangeVO> businessChangeList = reportMapper.businessChangeStatistics(dateDTO.getBeginCreateTime(), dateDTO.getEndCreateTime());
        Integer businessNum = businessMapper.selectCount(null);
        for (businessChangeVO b : businessChangeList) {
            b.setRadio(b.getNum().doubleValue()*100/businessNum);
        }
        return businessChangeList;
    }
}
